home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / misc / Fudgit233.lha / Source / src / dld / dl / dl_loadmod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-14  |  4.8 KB  |  200 lines

  1. /***********************************************************
  2. Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  3. Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /*
  25. ** An emulation of Jack's dld interface on top of GNU's dld.
  26. ** THIS ONLY WORKS IF THE CALLING PROGRAM IS *LINKED* WITH GCC!!!
  27. */
  28.  
  29. #include <stdio.h>
  30. #include <ctype.h>
  31.  
  32. #include "dl.h"
  33. #include "dld.h"
  34.  
  35. #define D(x)
  36.  
  37. static int inited;
  38.  
  39. dl_funcptr
  40. dl_loadmod(thisfile, loadfile, entrypoint)
  41.     char *thisfile, *loadfile, *entrypoint;
  42. {
  43.     int err;
  44.     char *libs;
  45.     dl_funcptr func;
  46.     
  47.     if (!inited) {
  48.         err = dld_init(dl_getbinaryname(thisfile));
  49.         if (err) {
  50.             dld_perror("Internal error");
  51.             dl_error("dl_loadmod: dld_init of %s failed", thisfile);
  52.             return 0;
  53.         }
  54.         inited = 1;
  55.     }
  56.  
  57.     D(fprintf(stderr, "calling dld_link(%s)\n", loadfile));
  58.     err = dld_link(loadfile);
  59.     if (err) {
  60.         dld_perror("Internal error");
  61.         dl_error("dl_loadmod: dld_link of %s failed", loadfile);
  62.         return 0;
  63.     }
  64.  
  65.     libs = dl_findlibs(loadfile);
  66.     if (libs) {
  67.         char *p, *q;
  68.         int i;
  69.         char buf[1024];
  70.         D(fprintf(stderr, "libs: '%s'\n", libs));
  71.         p = libs;
  72.         for (;;) {
  73.             while (*p && isspace(*p))
  74.                 p++;
  75.             if (!*p)
  76.                 break;
  77.             q = p;
  78.             while (*q && !isspace(*q))
  79.                 q++;
  80.             i = q-p;
  81.             strncpy(buf, p, i);
  82.             buf[i] = '\0';
  83.             D(fprintf(stderr, "calling dld_link(%s)\n", buf));
  84.             err = dld_link(buf);
  85.             if (err && err != DLD_EUNDEFSYM) {
  86.                 dld_perror("Internal error");
  87.                 dl_error("dl_loadmod: dld_link of library %s failed", buf);
  88.             }
  89.             p = q;
  90.         }
  91.     }
  92.  
  93.     if (dld_undefined_sym_count) {
  94.         int i;
  95.         char **pp;
  96.         pp = dld_list_undefined_sym();
  97.         for (i = 0; i < dld_undefined_sym_count; i++) {
  98.             fprintf(stderr, "\t%s\n", pp[i]);
  99.         }
  100.         free(pp);
  101.         dl_error("dl_loadmod: %d undefined symbols remain",
  102.             (char *) dld_undefined_sym_count);
  103.         return(0);
  104.     }
  105.     
  106.     if (!dld_function_executable_p(entrypoint)) {
  107.         dl_error("dl_loadmod: function %s not executable", entrypoint);
  108.     }
  109.     
  110.     D(fprintf(stderr, "call dld_get_func(%s)\n", entrypoint));
  111.     func = (dl_funcptr) dld_get_func(entrypoint);
  112.     if (func == 0)
  113.         dl_error("dl_loadmod: function %s not found", entrypoint);
  114.  
  115.     return(func);
  116. }
  117.  
  118. int
  119. dl_loadmod_mult(thisfile, loadfile, nl)
  120.     char *thisfile, *loadfile;
  121.     struct nlist nl[];
  122. {
  123.     int err, i, n;
  124.     char *libs;
  125.     dl_funcptr func;
  126.     
  127.     if (!inited) {
  128.         err = dld_init(dl_getbinaryname(thisfile));
  129.         if (err) {
  130.             dld_perror("Internal error");
  131.             dl_error("dl_loadmod_mult: dld_init of %s failed", thisfile);
  132.             return(0);
  133.         }
  134.         inited = 1;
  135.     }
  136.  
  137.     D(fprintf(stderr, "calling dld_link(%s)\n", loadfile));
  138.     err = dld_link(loadfile);
  139.     if (err) {
  140.         dld_perror("Internal error");
  141.         dl_error("dl_loadmod_mult: dld_link of %s failed", loadfile);
  142.         return(0);
  143.     }
  144.  
  145.     libs = dl_findlibs(loadfile);
  146.     if (libs) {
  147.         char *p, *q;
  148.         int i;
  149.         char buf[1024];
  150.         D(fprintf(stderr, "libs: '%s'\n", libs));
  151.         p = libs;
  152.         for (;;) {
  153.             while (*p && isspace(*p))
  154.                 p++;
  155.             if (!*p)
  156.                 break;
  157.             q = p;
  158.             while (*q && !isspace(*q))
  159.                 q++;
  160.             i = q-p;
  161.             strncpy(buf, p, i);
  162.             buf[i] = '\0';
  163.             D(fprintf(stderr, "calling dld_link(%s)\n", buf));
  164.             err = dld_link(buf);
  165.             if (err && err != DLD_EUNDEFSYM) {
  166.                 dld_perror("Internal error");
  167.                 dl_error("dl_loadmod_mult: dld_link of library %s failed", buf);
  168.             }
  169.             p = q;
  170.         }
  171.     }
  172.  
  173.     if (dld_undefined_sym_count) {
  174.         int i;
  175.         char **pp;
  176.         pp = dld_list_undefined_sym();
  177.         for (i = 0; i < dld_undefined_sym_count; i++) {
  178.             fprintf(stderr, "\t%s\n", pp[i]);
  179.         }
  180.         free(pp);
  181.         dl_error("dl_loadmod_mult: %d undefined symbols remain",
  182.              (char *) dld_undefined_sym_count);
  183.         return(0);
  184.     }
  185.     
  186.     for (i = n = 0; nl[i].n_name != 0; i++) {
  187.         if (!dld_function_executable_p(nl[i].n_name)) {
  188.             nl[i].n_value = 0;
  189.             nl[i].n_type = 0;
  190.             continue;
  191.         }
  192.         
  193.         D(fprintf(stderr, "call dld_get_func(%s)\n", nl[i].n_name));
  194.         nl[i].n_value = (unsigned long) dld_get_func(nl[i].n_name);
  195.         nl[i].n_type = 1;
  196.         n += (nl[i].n_value != 0);
  197.     }
  198.     return(n);
  199. }
  200.